package com.clas.liu.myapplication.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.view.activity.impl.CategoryActivity;
import com.clas.liu.myapplication.view.widget.CircleImageView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liu on 2019/3/3.
 */

public class HomeCategoryAdapter extends Adapter<RecyclerView.ViewHolder> {

    private int CATEGORY_COUNT = 6;
    private Context context;
    private List<String> category = new ArrayList<String>();
    private List<List<String>> headList;

    public HomeCategoryAdapter(Context context,List<List<String>> headList) {
        this.context = context;
        this.headList = headList;
        category.add("闲置出售");
        category.add("失物招领");
        category.add("求助帮忙");
        category.add("寻人交友");
        category.add("校园吐槽");
        category.add("兼职广告");

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MyHolder holder = new MyHolder(LayoutInflater.from(context).
                inflate(R.layout.item_home_layout, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

        MyHolder holder = (MyHolder) viewHolder;
        holder.categoryName.setText(category.get(position));
        if (headList.size() - 1 >= position) {
            List<String> heads = headList.get(position);
            if (heads != null && heads.size() > 0)
                for (int i = 0; i < heads.size(); i++) {
                    String url = heads.get(i);
                    holder.heads.get(i).setVisibility(View.VISIBLE);
                    Picasso.with(context).load(url).placeholder(R.mipmap.loading).error(R.mipmap.loading)
                            .into(holder.heads.get(i));
                }
        }
        final int loc = position;
        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入分类页面
                Intent intent = new Intent(context, CategoryActivity.class);
                intent.putExtra("category",loc);
                context.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return CATEGORY_COUNT;
    }

    public void updateData(List<List<String>> heads) {
       this.headList = heads;
        this.notifyDataSetChanged();
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        TextView categoryName;
        LinearLayout layout;
        RelativeLayout more;
        List<CircleImageView> heads;

        public MyHolder(View itemView) {
            super(itemView);
            categoryName = itemView.findViewById(R.id.home_category_name);
            more = itemView.findViewById(R.id.home_rv_more_layout);
            layout = itemView.findViewById(R.id.home_item_layout);
            heads = new ArrayList<>();
            heads.add((CircleImageView) itemView.findViewById(R.id.home_head1));
            heads.add((CircleImageView) itemView.findViewById(R.id.home_head2));
            heads.add((CircleImageView) itemView.findViewById(R.id.home_head3));
            heads.add((CircleImageView) itemView.findViewById(R.id.home_head4));
            heads.add((CircleImageView) itemView.findViewById(R.id.home_head5));
        }
    }
}
